package cn.edu.sgu.www.pms.controller;

import cn.edu.sgu.www.pms.annotation.FeignService;
import cn.edu.sgu.www.pms.consts.CustomConstants;
import cn.edu.sgu.www.pms.consts.CustomMessages;
import cn.edu.sgu.www.pms.consts.MimeTypes;
import cn.edu.sgu.www.pms.dto.PermissionDTO;
import cn.edu.sgu.www.pms.dto.PermissionInitDTO;
import cn.edu.sgu.www.pms.entity.Permission;
import cn.edu.sgu.www.pms.pager.impl.PermissionPager;
import cn.edu.sgu.www.pms.restful.JsonPage;
import cn.edu.sgu.www.pms.restful.JsonResult;
import cn.edu.sgu.www.pms.service.PermissionService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.Set;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "权限管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@Validated
@RestController
@RequestMapping(path = "/permission", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class PermissionController {

    private final PermissionService permissionService;

    @Autowired
    public PermissionController(PermissionService permissionService) {
        this.permissionService = permissionService;
    }

    @ApiOperation("权限数据初始化")
    @RequestMapping(value = "/init", method = RequestMethod.POST)
    public JsonResult<Void> init() throws ClassNotFoundException {
        permissionService.init();

        return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
    }

    @ApiOperation("添加权限")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult<Void> insert(@Validated PermissionDTO permissionDTO) {
        permissionService.insert(permissionDTO);

        return JsonResult.success(CustomMessages.INSERT_SUCCESS);
    }

    @ApiOperation("通过ID批量删除权限")
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
    public JsonResult<Void> deleteByIds(
            @ApiParam(name = "ids", value = "权限ID列表", required = true)
            @Size(min = 1, message = "权限ID列表不能为空")
            @RequestParam("ids") List<String> ids) {
        permissionService.deleteByIds(ids);

        return JsonResult.success(CustomMessages.DELETE_SUCCESS);
    }

    @ApiOperation("通过ID修改权限信息")
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public JsonResult<Void> updateById(@Validated PermissionDTO permissionDTO) {
        permissionService.updateById(permissionDTO);

        return JsonResult.success(CustomMessages.UPDATE_SUCCESS);
    }

    @ApiOperation("通过ID查询权限信息")
    @RequestMapping(value = "/selectById", method = RequestMethod.GET)
    public JsonResult<Permission> selectById(
            @ApiParam(name = "id", value = "权限ID", required = true)
            @NotBlank(message = CustomMessages.ID_NOT_NULL) String id) {
        Permission permission = permissionService.selectById(id);

        return JsonResult.success(null, permission);
    }

    @ApiOperation("查询全部父级权限")
    @RequestMapping(value = "/selectParent", method = RequestMethod.GET)
    public JsonResult<List<Permission>> selectParent() {
        List<Permission> list = permissionService.selectParent();

        return JsonResult.success(null, list);
    }

    @ApiOperation("查询权限列表")
    @RequestMapping(value = "/selectList", method = RequestMethod.GET)
    public JsonResult<List<Permission>> selectList() {
        List<Permission> list = permissionService.selectList();

        return JsonResult.success(null, list);
    }

    @ApiOperation("分页查询权限列表")
    @RequestMapping(value = "/selectByPage", method = RequestMethod.GET)
    public JsonResult<JsonPage<Permission>> selectByPage(PermissionPager pager) {
        Page<Permission> selectByPage = permissionService.selectByPage(pager);

        return JsonResult.restPage(selectByPage);
    }

    @FeignService
    @ApiOperation("权限数据初始化")
    @RequestMapping(value = "/resources", method = RequestMethod.POST)
    public JsonResult<Void> resources(@RequestBody PermissionInitDTO permissionInitDTO) {
        permissionService.resources(permissionInitDTO);

        return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
    }

    @FeignService
    @ApiOperation("查询应用的非匿名子权限")
    @RequestMapping(value = "/selectPermissions", method = RequestMethod.GET)
    public JsonResult<List<Permission>> selectPermissions(
            @ApiParam(name = "service", value = "应用名", required = true)
            @NotBlank(message = "应用名" + CustomMessages.NOT_NULL) String service) {
        List<Permission> list = permissionService.selectPermissions(service);

        return JsonResult.success(null, list);
    }

    @FeignService
    @ApiOperation("通过用户名查询用户权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true),
            @ApiImplicitParam(name = "service", value = "应用名", required = true)
    })
    @RequestMapping(value = "/selectUserPermissions", method = RequestMethod.GET)
    public JsonResult<Set<String>> selectUserPermissions(
            @NotBlank(message = "用户名" + CustomMessages.NOT_NULL) String username,
            @NotBlank(message = "应用名" + CustomMessages.NOT_NULL) String service) {
        Set<String> set = permissionService.selectUserPermissions(username, service);

        return JsonResult.success(null, set);
    }

}